package com.coder;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.coder.config.HttpUtils;
import com.coder.entity.MajorSat;
import com.coder.service.MajorSatService;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Random;

/**
 * @author 王令
 */
@SpringBootTest
public class MajorSatReptile {
    @Autowired
    private MajorSatService majorSatService;

    @Test
    void MajorSatReptile() throws IOException {
        for (int p = 0; p < 131; p++){

            Element bodyElement = HttpUtils.getBodyElement("https://gaokao.chsi.com.cn/zyk/pub/myd/specAppraisalTop.action?start=" + 20 * p);
            Elements trs = bodyElement.select("#queryResult > table.cnt_table > tbody > tr");
            for (Element tr : trs) {
                Elements tds = tr.select("td");
                String coll = tds.get(0).text();
                String level = tds.get(1).text();

                Element major = HttpUtils.getBodyElement("https://gaokao.chsi.com.cn" + tds.get(6).select("a").attr("href"));
                Elements major_trs = major.select("#queryResult > table > tbody> tr");

                for (Element major_tr : major_trs) {
                    Elements major_tds = major_tr.select("td");
                    String majorStr = major_tds.get(0).text();
                    String sat = major_tds.get(1).text();
                    majorSatService.save(new MajorSat().setColleges(coll).setLevel(level).setMajor(majorStr).setMajorSat(sat.split(" ")[0]));
                }

            }
        }
    }


    @Test
    void test(){
        Random random = new Random();
        majorSatService.update(new MajorSat().setColleges("抚顺职业技术学院"), new LambdaQueryWrapper<MajorSat>().eq(MajorSat::getColleges, ""));
    }
}
